package com.example.core.model;


import com.example.core.convert.ModelToFormPropertyMap;
import com.example.core.convert.model.ModelConvertor_ModelListToString;
import com.example.core.convert.model.ModelConvertor_ModelToString;
import com.example.core.form.BaseAuthForm;
import com.example.sys.authority.model.SysAuthority;
import com.example.sys.org.model.SysOrgElement;
import lombok.Getter;
import lombok.Setter;

import javax.persistence.JoinColumn;
import javax.persistence.MappedSuperclass;
import javax.persistence.OneToOne;
import java.util.ArrayList;
import java.util.List;

@Getter
@Setter
@MappedSuperclass
public abstract class BaseAuthModel extends BaseModel<BaseAuthModel> {

    @OneToOne
    @JoinColumn(name = "fd_creator_id", referencedColumnName = "fd_id")
    private SysOrgElement fdCreator;

    @OneToOne
    @JoinColumn(name = "fd_modifier_id", referencedColumnName = "fd_id")
    private SysOrgElement fdModifier;

    public transient List<SysAuthority> sysReaders = new ArrayList<>();

    public transient List<SysAuthority> sysEditers = new ArrayList<>();

    public transient List<SysAuthority> sysDeleters = new ArrayList<>();

    @Override
    public ModelToFormPropertyMap getToFormPropertyMap() {
        ModelToFormPropertyMap toFormPropertyMap = super.getToFormPropertyMap();
        toFormPropertyMap.put("fdCreator.fdId", new ModelConvertor_ModelToString("fdCreatorId"));
        toFormPropertyMap.put("fdCreator.fdName", new ModelConvertor_ModelToString("fdCreatorName"));
        toFormPropertyMap.put("fdModifier.fdId", new ModelConvertor_ModelToString("fdModifierId"));
        toFormPropertyMap.put("fdModifier.fdName", new ModelConvertor_ModelToString("fdModifierName"));
        toFormPropertyMap.put("sysReaders", new ModelConvertor_ModelListToString("fdReaderIds:fdReaderNames", "fdId:fdName"));
        toFormPropertyMap.put("sysEditers", new ModelConvertor_ModelListToString("fdEditersIds:fdEditersNames", "fdId:fdName"));
        toFormPropertyMap.put("sysDeleters", new ModelConvertor_ModelListToString("fdDeletersIds:fdDeletersNames", "fdId:fdName"));
        return toFormPropertyMap;
    }

    @Override
    public Class<?> getFormClass() {
        return BaseAuthForm.class;
    }
}
